package com.sh.cathtml.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sh.suhe.utildemos.R
import com.sh.suhe.utildemos.model.MainModel
import java.util.ArrayList

class TopAdapter: RecyclerView.Adapter<TopAdapter.ViewHolder>(){
    lateinit var onClick: TopAdapter.OnClick
    var datas = ArrayList<MainModel>()
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): TopAdapter.ViewHolder {
        val view = LayoutInflater.from(p0.context).inflate(R.layout.adapter_top_item,null)

       return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onBindViewHolder(viewHolder: TopAdapter.ViewHolder, position: Int) {
        val item = datas[position]
        viewHolder.topName.text = item.title
        viewHolder.topName.setOnClickListener { onClick.toCatalogue(item) }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var topName:TextView = itemView.findViewById(R.id.top_name)
    }
    fun notifyDataSetChanged(datas:List<MainModel>){
        if ( datas.isNotEmpty()){
            this.datas.addAll(datas)
            notifyDataSetChanged()
        }
    }
    public interface OnClick{
        fun toCatalogue(item: MainModel)
    }
}